package com.iannotation.demo.aspect;

import com.iannotation.demo.annotaion.EncryCertNo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @Description: 身份证格式校验 - 注解处理器
 * @Author: Sean
 * @Date: 2020/5/20 10:34
 */
public class EncryCertNoContraintValidated implements ConstraintValidator<EncryCertNo, Object> {

    private static final Logger logger = LoggerFactory.getLogger(EncryCertNoContraintValidated.class);

    /**
     * 身份证正则：
     * 十八位： ^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
     * 十五位： ^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$
     */
    private static final String certNoEighteen = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
    private static final String certNoFifteen = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$";

    @Override
    public void initialize(EncryCertNo constraintAnnotation) {
        if (logger.isInfoEnabled()) {
            logger.info("EncryCertNoContraintValidated init... ...");
        }
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        if (logger.isInfoEnabled()) {
            logger.info("EncryCertNoContraintValidated isValid...... " + o);
        }
        if (!ObjectUtils.isEmpty(o)) {
            String certNo = String.valueOf(o);
            if (Pattern.matches(certNoEighteen, certNo) || Pattern.matches(certNoFifteen, certNo)) {
                return true;
            }
            return false;
        }
        return false;
    }
}
